﻿using Experimental.System.Messaging;
using System;
using System.Collections.Generic;
using System.Text;

namespace Wicture.EPC.Common.Biz
{
    public class MsMqHelper
    {
        //string path = "FormatName:Direct=TCP:192.168.0.144\\private$\\vin";
        private readonly string _path;
        public MsMqHelper(string path)
        {
            _path = path;
        }
        public T GetMessage<T>()
        {
            T result = default(T);
            MessageQueue myQueue = new MessageQueue(_path);
            try
            {
                var message = myQueue.Receive(TimeSpan.FromSeconds(3));
                result = (T)message.Body;
            }
            catch
            {

            }
            return result;
        }

        public bool PushMessage<T>(T msg)
        {
            bool result = true;
            MessageQueue myQueue = new MessageQueue(_path);
            try
            {
                Message ms = new Message();
                ms.Body = msg;
                ms.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
                myQueue.Send(ms);
            }
            catch
            {
                result = false;
            }
            return result;
        }
    }
}
